package com.cloud.thelen.wallpaper;

import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cloud.thelen.wallpaper.Adapter.WallpaperAdapter;
import com.cloud.thelen.wallpaper.BaseUI.BaseFragment;
import com.cloud.thelen.wallpaper.Data.Link;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class HomeFragment extends BaseFragment {

    private RecyclerView rv1, rv2;
    private View view;

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        this.view = view;
        rv1 = view.findViewById(R.id.rv1);
        rv2 = view.findViewById(R.id.rv2);
    }

    private List<String> links(List<String> strings) {
        int i = 0;
        Set<Integer> set = new HashSet<>();
        while (i < 30) {
            int random = new Random().nextInt(strings.size());
            set.add(random);
            i = set.size();
        }
        List<String> links = new ArrayList<>();
        for (Iterator<Integer> iterator = set.iterator(); iterator.hasNext(); ) {
            links.add(strings.get(iterator.next()));
        }
        return links;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void setEvent() {
        List<String> strings = links(Link.getAll());
        rv1.setLayoutManager(new GridLayoutManager(getContext(), 4) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        rv2.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        WallpaperAdapter adapter1 = new WallpaperAdapter(getActivity(), 1);
        WallpaperAdapter adapter2 = new WallpaperAdapter(getActivity(), 2);
        rv1.setAdapter(adapter1);
        rv2.setAdapter(adapter2);
        adapter1.setData(arrayList(strings.subList(0, 8)));
        adapter2.setData(arrayList(strings.subList(9, 30)));
    }

    @Override
    protected void getData() {

    }

    private ArrayList<String> arrayList(List<String> strings) {
        ArrayList<String> strings1 = new ArrayList<>();
        strings1.addAll(strings);
        return strings1;
    }

}
